/*
 * 求数组中的最大值和最小值
 *
 */

#include <stdio.h>
#include <string.h>

//解法一
int find_max_and_min(int* array, int num, int *max, int *min);
//解法二：分治法
int find_max_and_min_other(int* array, int num, int *max, int *min);

int find_max_and_min(int* array, int num, int *max, int *min)
{
    if (num & 1) {
        *max = array[num-1];
        *min = array[num-1];
        num--;
    }
    else {
        *max = 0;
        *min = 0;
    }
	for (int i = 0; i < num-1; i += 2) {
        if (array[i] < array[i+1]) {
            if (*max < array[i+1])
                *max = array[i+1];
            if (*min > array[i])
                *min = array[i];
        }
        else {
            if (*max < array[i])
                *max = array[i];
            if (*min > array[i+1])
                *min = array[i+1];
        }
    }
    return 0;
}

int find_max_and_min_other(int* array, int num, int *max, int *min)
{
    return 0;
}

int main()
{
    int a[] = {6, 5, 8, 3, 9, 7};
    printf("%d\n", find_max_and_min(a, b));
}


